package com.icinema.main;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.icinema.client.Message;
import com.icinema.client.RatingService;
import com.icinema.client.RatingServiceAsync;
import com.icinema.vo.Rating;

public class CreateRatingView extends Composite {
	private final RatingServiceAsync ratingService = GWT
	.create(RatingService.class);
	TextBox txtRatingDescription = new TextBox();
	FlexTable ratingTable = new FlexTable();
	public CreateRatingView(){
		VerticalPanel panel = new VerticalPanel();
		initWidget(panel);
		FlexTable grid = new FlexTable();
		grid.getCellFormatter().setStyleName(1,1,"createRatingOkButton");
		grid.setWidget(0,0,new Label("Rating Name:"));
		grid.setWidget(0,1,txtRatingDescription);
		txtRatingDescription.setStyleName("textBox");
		Button okButton = new Button("OK");
		grid.setWidget(1,1,okButton);
		okButton.addMouseUpHandler(new MouseUpHandler(){
			
			@Override
			public void onMouseUp(MouseUpEvent event) {
				ratingService.addNewRating(txtRatingDescription.getText(),
						new AsyncCallback<String>(){
							@Override
							public void onFailure(Throwable caught) {
							}

							@Override
							public void onSuccess(String result) {
								txtRatingDescription.setText("");
								refresh();
							}
				}
				);
			}
			
		});
		panel.add(grid);
		panel.add(ratingTable);
	}
	public void refresh(){
		this.setVisible(false);
		this.setVisible(true);
	}
	class ImageDeleteMouseDownHandler implements  MouseDownHandler{
		private String movieShowingId; 
		public ImageDeleteMouseDownHandler(String movieShowingId){
			this.movieShowingId = movieShowingId;
		}
		@Override
		public void onMouseDown(MouseDownEvent event) {
			if(!Window.confirm("This will Delete the Rating")){
				return;
			}
			ratingService.deleteRating(movieShowingId, new AsyncCallback<Message>(){

				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(Message result) {
					if(!result.getErr_msg().equals("")){
						Window.alert(result.getErr_msg());
					}
				}
			});
			refresh();
		}
	}		
	
	@Override
	public void setVisible(boolean visible) {
		if(visible == true){
			ratingTable.removeAllRows();
			ratingTable.getRowFormatter().setStyleName(0,"tableViewBrowseHeaders");
			ratingTable.setText(0,0,"Current Ratings");
			ratingService.getRatings(new AsyncCallback<List<Rating>>(){
				@Override
				public void onFailure(Throwable caught) {
					
				}

				@Override
				public void onSuccess(List<Rating> result) {
					for(int i=0;i<result.size();i++){
						Image imgDelete = new Image("/images/delete.png");
						imgDelete.setStyleName("imgButton");
						String ratingId = result.get(i).getId();
						imgDelete.addMouseDownHandler(new ImageDeleteMouseDownHandler(ratingId));
						ratingTable.setWidget(i+1,1,imgDelete);
						ratingTable.setText(i+1,0,result.get(i).getName());
						ratingTable.getCellFormatter().setStyleName(i+1, 0, "tableCell");
						
					}
				}
			});			
		}
		super.setVisible(visible);
	}
	
}